﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.Business
{
    public class ManagerKardex
    {
        public static void NewKardex(Model.Kardex newKardex)
        {
            var feesAndFines = DataAccessFeesAndFines.GetCurrentFeesAndFines();
            if(feesAndFines.Count==0)
            {
                throw new Exception("No existen tarifas registradas porfavor registre las tarifas para poder registrar un kardex.");
            }
            ManagerPerson.UpdatePersonIfExisteElseCreate(newKardex.Person);
            newKardex.ClientId = newKardex.Person.PersonId;
            newKardex.Person = null;
            var allot=newKardex.AllotmentModel;
            newKardex.AllotmentModel=ManagerAllotment.GetAllotmentNotAssigned(allot);
            newKardex.Enable = true;
            newKardex.WaterMeasurer = string.Empty;
            DataAccessKardex.CreateKardex(newKardex);
            var newControl=new OtherCollectControl{KardexId = newKardex.KardexId,CollectType = 0, ControlDetail = "Nueva Conexion", PaymentAmount = feesAndFines.First().NewConection, 
                DateGenerated = DateTime.Now,
            IsPaid = false};

        }

        public static void VerifyDataPersonIsCorrect(Model.Kardex mp)
        {
            if (string.IsNullOrWhiteSpace(mp.Person.PersonId)) 
                throw new Exception("El Ci Ingresado es incorrecto porfavor reintente.");
            if (string.IsNullOrWhiteSpace(mp.Person.Names))
                throw new Exception("Para poder {0} al socio necesita tener nombres.");
            if (string.IsNullOrWhiteSpace(mp.Person.LastNames))
                throw new Exception("Para poder {0} al socio necesita tener apellidos.");
            
        }

        public static void VerifyDataPersonIsKardexWithoutPerson(Model.Kardex mp)
        {
            if (mp.Allotment.Count==0)
            {
                throw new Exception("Para poder {0} el kardex debe seleccionar el manzano y lote del socio.");
            }
            if (string.IsNullOrEmpty(mp.AllotmentModel.SquareId) || string.IsNullOrEmpty(mp.AllotmentModel.AllotmentName))
            {
                throw new Exception("Para poder {0} el kardex debe seleccionar el manzano y lote del socio.");
            }
            if (ManagerAllotment.GetAllotmentNotAssignedWithoutKardex(mp) == null)
            {
                throw new Exception("El lote que desea asignar al kardex ya esta designado a un kardex habilitado.");
            }

        }

        public static ICollection<Model.Kardex> SearchKardexByParameter(Model.Kardex kardexFind, string state)
        {
            ICollection<Model.Kardex> k;
            switch (state)
            {
                case ("Activo"):

                    k = DataAccessKardex.SearchKardexByParameterEnable(kardexFind);
                    break;
                case ("Inactivo"):

                    k = DataAccessKardex.SearchKardexByParameterDisable(kardexFind);
                    break;
                default :
                    k = DataAccessKardex.SearchKardexByParameterAll(kardexFind);
                    break;
            }
            if (k.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            return k;
        }

        public static ICollection<Model.Kardex> SearchKardexByParameter(Model.KardexHelper kardex)
        {
            ICollection<Model.Kardex> kardexFinded = DataAccessKardex.SearchKardexByParameterEnable(kardex);
            if (kardexFinded.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            return kardexFinded;
        }

        public static void UpdateKardex(Model.Kardex modifyKardex)
        {
            ManagerPerson.UpdatePersonIfExisteElseCreate(modifyKardex.Person);
            modifyKardex.ClientId = modifyKardex.Person.PersonId;
            modifyKardex.Person = null;
            var allot = modifyKardex.AllotmentModel;
            modifyKardex.AllotmentModel=ManagerAllotment.GetAllotmentNotAssigned(allot);
            DataAccessAllotment.DeleteKardexFromAllotment(modifyKardex);
            modifyKardex.AllotmentModel = ManagerAllotment.GetAllotmentNotAssigned(allot);
            DataAccessAllotment.AssignAllotmentToKardex(modifyKardex);
            DataAccessKardex.UpdateKardex(modifyKardex);
        }

        public static Model.Kardex GetKardexByKardexId(Model.Kardex kardex)
        {
            List<Model.Kardex> kardexes = DataAccessKardex.SearchKardexesByKardexId(kardex);
            if (kardexes.Count == 0)
                return null;
            else
                return kardexes.First();
        }

        public static Model.Kardex GetKardexByClientId(Model.Kardex kardex)
        {
            List<Model.Kardex> kardexes = DataAccessKardex.SearchKardexesByClientId(kardex);
            if (kardexes.Count == 0)
                return null;
            else
                return kardexes.First();
        }

        public static Allotment GetAllotmentByAllotmentNameAndSquareId(Allotment allotment)
        {
            List<Allotment> allots=DataAccessAllotment.SearchAllotmentByNameAndSquareId(allotment);
            if (allots.Count == 0)
                return null;
            else
                return allots.First();
        }

        public static void DisableKardex(Kardex KardexForDisable)
        {
            Model.Kardex k = GetKardexByKardexId(KardexForDisable);
            ManagerAllotment.DeleteKardexFromAllotment(KardexForDisable);
            DataAccessKardex.DisableKardex(KardexForDisable);
        }


        public static List<int> GetAllPosibleYearsOfKardexReadingControl(Kardex kardex)
        {
            return  new List<int>(){2012};
        }

        public static void CreateAndOpenKardex(Kardex kardex, int year)
        {
            DataAccessKardex.CreateReportKardexAndOpen(kardex, year);
        }
    }
}
